package day05;

import java.util.Random;

//   - 定义数组，包含10个元素，遍历数组，给每个元素赋值为0到99之间的随机数并输出，找到数组元素的最大值并输出
public class MaxOfArray {
    public static void main(String[] args) {
        int[] arr1 = getArray(5);
        printArray(arr1);
        System.out.println("===========");
        int[] arr2 = getArray(10);
        printArray(arr2);
        printMax(arr2);
    }

    public static int[] getArray(int index) {
        Random r = new Random();
        int[] arr = new int[index];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = r.nextInt(100);
        }
        return arr;
    }

    public static void printMax(int[] arr) {
        int max = 0;
        for (int i = 0; i < arr.length; i++) {
            if (max < arr[i]) {
                max = arr[i];
            }
        }
        System.out.println( "该数组的最大值为" + max);
    }

    public static void printArray(int[] arr) {
        System.out.print("[");
        for (int i = 0; i < arr.length; i++) {
            if (i == arr.length - 1) {
                System.out.print(arr[i]);
            } else {
                System.out.print(arr[i] + ", ");
            }
        }
        System.out.print("]");
        System.out.println();
    }
}
